﻿namespace LastVersionOfIpPackage.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Enum_needed;
    using ImageOperations;

    /// <summary>
    /// add noise class
    /// </summary>
    public partial class RemoveNoiseFreq : Form
    {
        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot = false;

        /// <summary>
        /// result image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// type of remove
        /// </summary>
        private FilterType type;

        /// <summary>
        /// the fourier Image 
        /// </summary>
        private Image freqImage;

        /// <summary>
        /// Initializes a new instance of the <see cref="RemoveNoiseFreq"/> class.
        /// </summary>
        public RemoveNoiseFreq()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>
        /// The type.
        /// </value>
        public FilterType Type
        {
            get { return this.type; }
            set { this.type = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not1].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not1]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot1
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the info of orignal image.
        /// </summary>
        /// <param name="image">The orginal image.</param>
        /// <param name="width">The width of orignal image.</param>
        /// <param name="height">The height of orignal image.</param>
        /// <param name="type">The type of remove use.</param>
        internal void SetInfo(Image image, string width, string height, FilterType type)
        {
            this.picOrg.Image = image;
            this.LabWdth.Text = width;
            this.LabHth.Text = height;
            this.type = type;
            if (this.type == FilterType.NotchPass || this.type == FilterType.NotchReject)
            {
                this.GrpNotch.Visible = true;
                Bitmap graphImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.ConvertToFreq((Bitmap)this.picOrg.Image);
                graphImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.Gamma(graphImg, 2);
                this.PbFFT.Image = graphImg;
                this.freqImage = (Image)graphImg;
            }
            else
            {
                this.GrpBand.Visible = true;
                Bitmap graphImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.ConvertToFreq((Bitmap)this.picOrg.Image);
                graphImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.Gamma(graphImg, 2);
                this.PbFFT.Image = graphImg;
                this.freqImage = (Image)graphImg;
            }
        }

        /// <summary>
        /// Handles the Click event of the BtCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtCancel_Click(object sender, EventArgs e)
        {
            this.saveOrNot = false;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.type == FilterType.NotchPass || this.type == FilterType.NotchReject)
                {
                    this.resultImg = ImageEnchance.NewImageEnchanemwnt.FilterOrRemoveNoiseFreq((Bitmap)this.picOrg.Image, this.type, TypeOfMask.Ideal, double.Parse(this.txtNotchRadius.Text), 0, double.Parse(this.txtUc.Text), double.Parse(this.txtVc.Text));
                }
                else
                {
                    this.resultImg = ImageEnchance.NewImageEnchanemwnt.FilterOrRemoveNoiseFreq((Bitmap)this.picOrg.Image, this.type, TypeOfMask.Ideal, double.Parse(this.txtD0.Text), double.Parse(this.txtWB.Text), 0, 0);
                }

                this.PicFilterImg.Image = this.resultImg;
                this.PbFFT.Image = this.freqImage;
                this.btSave.Enabled = true;
            }
            catch
            {
                if (this.picOrg.Image == null)
                {
                    MessageBox.Show("no image to make remove noise operation");
                }
                else
                {
                    MessageBox.Show("operation fail make sure your data");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtSave_Click(object sender, EventArgs e)
        {
            if (this.PicFilterImg.Image != null)
            {
                this.saveOrNot = true;
                this.Close();
            }
        }

        /// <summary>
        /// Handles the MouseMove event of the PbFFT control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void PbFFT_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Cross;
            if (this.type == FilterType.NotchPass || this.type == FilterType.NotchReject)
            {
                this.txtUc.Text = (e.X - (this.picOrg.Image.Width / 2)).ToString();
                this.txtVc.Text = (e.Y - (this.picOrg.Image.Height / 2)).ToString();
            }
        }

        /// <summary>
        /// Handles the MouseMove event of the RemoveNoiseFreq control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void RemoveNoiseFreq_MouseMove(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// Handles the MouseClick event of the PbFFT control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void PbFFT_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.type == FilterType.NotchPass || this.type == FilterType.NotchReject)
            {
                try
                {
                    float radios = float.Parse(this.txtNotchRadius.Text);
                    Graphics g;
                    Bitmap drawArea = new Bitmap(this.freqImage);
                    g = Graphics.FromImage(drawArea);
                    Pen mypen = new Pen(Color.Red, float.Parse(this.txtNotchRadius.Text));
                    g.DrawEllipse(mypen, e.X - (radios / 2), e.Y - (radios / 2), float.Parse(this.txtNotchRadius.Text), float.Parse(this.txtNotchRadius.Text));
                    this.PbFFT.Image = drawArea;
                    g.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnDraw control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnDraw_Click(object sender, EventArgs e)
        {
            try
            {
                Graphics g;
                Bitmap drawArea = new Bitmap(this.freqImage);
                g = Graphics.FromImage(drawArea);
                Pen mypen = new Pen(Color.Red, float.Parse(this.txtWB.Text));
                g.DrawEllipse(mypen, (this.picOrg.Width / 2) - float.Parse(this.txtD0.Text), (this.picOrg.Height / 2) - float.Parse(this.txtD0.Text), float.Parse(this.txtD0.Text) * 2, float.Parse(this.txtD0.Text) * 2);
                this.PbFFT.Image = drawArea;
                g.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error please enter data");
            }
        }
    }
}
